<template>
	<view v-if='activity != ""'>
		<view style='padding:10px'>
			<u-parse :content="activity.description"></u-parse>
		</view>
		<view style='height:30px'>

		</view>
		<view class='footer'>
			<view style='padding:10px'>
				<u-button v-if='isTimeOut' type='error' text='报名已结束' shape='circle' :plain='true' :disabled='true'></u-button>
				<u-button v-else type='success' :text='btnText' shape='circle' :plain='true' @click='show = true'></u-button>
			</view>
		</view>
		<view>
			<u-popup :show="show" @close="show = false" mode='bottom'>
				<view style='padding:10px'>
					<u-divider text="请填写报名信息"></u-divider>
					<u--form labelWidth='80px'>
						<u-form-item label="姓名">
							<u--input v-model="record.name"></u--input>
						</u-form-item>
						<u-form-item label="电话">
							<u--input v-model="record.mobile"></u--input>
						</u-form-item>
						<u-form-item label="身份证">
							<u--input v-model="record.description"></u--input>
						</u-form-item>
					</u--form>
						<u-button type='primary' text='确认报名'  @click='submit'></u-button>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import wechatPay from '@/utils/wechat-pay.js'
	export default {
		data() {
			return {
				activity: '',
				btnText: '马上报名',
				show: false,
				record: {
					name: '',
					description: '',
					mobile: '',
					cativity: 0
				},
				isTimeOut: false
			}
		},
		onLoad(options) {
			if (typeof(options.id) != 'undefined') {
				uni.showLoading()
				this.request.get(`public/activities/${options.id}`).then((res) => {
					console.log(res.data)
					this.record.activity = res.data.id
					if (res.data && res.data.title) {
						this.activity = res.data
						uni.setNavigationBarTitle({
							title: res.data.title
						})
						let date = new Date()
						let startTime = new Date(res.data.startTime)
						if(date.getTime() > startTime.getTime()){
							this.isTimeOut = true
						}else{
							this.btnText = `马上报名(￥${res.data.price})`
						}
					}
					uni.hideLoading()
				})
			} else {
				uni.showModal({
					title: '',
					content: '参数错误',
					showCancel: false,
					success: () => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				})
			}
		},
		onShow() {

		},
		methods: {
			submit(){
				let token = uni.getStorageSync('api_token')
				if(token == ''){
					uni.showModal({
						title: '',
						content: '请先登录',
						showCancel: false,
						success: () => {
							uni.navigateTo({
								url: '/pages/index/login'
							})
						}
					})
				}else{
					if(this.record.name.trim() == '' || this.record.mobile.trim() == '' || this.record.description.trim() == ''){
						uni.showToast({
							title: '请填写相关信息',
							icon: 'none'
						})
						return
					}
					this.request.post(`api/activity-records`,this.record).then((res) => {
						this.show = false
						let invoiceId = res.data.invoice.id
						uni.navigateTo({
							url: `/pages/payment/pay-code?invoice=${invoiceId}`
						})
						// wechatPay(invoiceId).then((res) => {
						// 	uni.hideLoading()
						// 	uni.showModal({
						// 		title: '',
						// 		content: '支付成功！',
						// 		showCancel: false,
						// 		success: () => {
						// 			uni.navigateTo({
						// 				url: '/pages/mine/activity-record'
						// 			})
						// 		}
						// 	})
						// }).catch((err) => {
						// 	uni.hideLoading()
						// 	uni.showModal({
						// 		title: '',
						// 		content: '支付失败，请重试',
						// 		showCancel: false,
						// 		success: () => {
						// 			uni.navigateTo({
						// 				url: '/pages/mine/activity-record'
						// 			})
						// 		}
						// 	})
						// })
					})
				}
			}
		}
	}
</script>

<style>

</style>